package org.rsbot.bot;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.lang.reflect.Constructor;
import java.util.EventListener;
import java.util.Map;
import java.util.TreeMap;
import org.rsbot.Application;
import org.rsbot.client.Client;
import org.rsbot.client.input.Canvas;
import org.rsbot.event.EventManager;
import org.rsbot.event.events.PaintEvent;
import org.rsbot.event.events.TextPaintEvent;
import org.rsbot.gui.AccountManager;
import org.rsbot.script.internal.BreakHandler;
import org.rsbot.script.internal.InputManager;
import org.rsbot.script.internal.ScriptHandler;
import org.rsbot.script.methods.Menu;
import org.rsbot.script.methods.MethodContext;

public class Bot
{
  private String account;
  private BotStub botStub;
  private Client client;
  private MethodContext methods;
  private Component panel;
  private PaintEvent paintEvent;
  private TextPaintEvent textPaintEvent;
  private EventManager eventManager;
  private BufferedImage backBuffer;
  private BufferedImage image;
  private InputManager im;
  private RSLoader loader;
  private ScriptHandler sh;
  private BreakHandler bh;
  private Map<String, EventListener> listeners;
  public volatile boolean overrideInput = false;

  public volatile boolean disableRandoms = false;

  public volatile boolean disableAutoLogin = false;

  public volatile boolean disableRendering = false;

  public volatile int inputFlags = 3;

  public Bot() {
    this.im = new InputManager(this);
    this.loader = new RSLoader();
    Dimension localDimension = Application.getPanelSize();
    this.loader.setCallback(new Runnable(localDimension) {
      public void run() {
        try {
          Bot.this.setClient((Client)Bot.this.loader.getClient());
          Bot.this.resize(this.val$size.width, this.val$size.height);
          Bot.this.methods.menu.setupListener();
        }
        catch (Exception localException)
        {
        }
      }
    });
    this.sh = new ScriptHandler(this);
    this.bh = new BreakHandler();
    this.backBuffer = new BufferedImage(localDimension.width, localDimension.height, 1);
    this.image = new BufferedImage(localDimension.width, localDimension.height, 1);
    this.paintEvent = new PaintEvent();
    this.textPaintEvent = new TextPaintEvent();
    this.eventManager = new EventManager();
    this.listeners = new TreeMap();
  }

  public void start() {
    try {
      this.loader.paint(this.image.getGraphics());
      this.loader.load();
      if (this.loader.getTargetName() == null) {
        return;
      }
      this.botStub = new BotStub(this.loader);
      this.loader.setStub(this.botStub);
      this.eventManager.start();
      this.botStub.setActive(true);
      ThreadGroup localThreadGroup = new ThreadGroup("RSClient-" + hashCode());
      Thread localThread = new Thread(localThreadGroup, this.loader, "Loader");
      localThread.start();
    } catch (Exception localException) {
    }
  }

  public void stop() {
    this.eventManager.killThread(false);
    this.sh.stopScript();
    this.loader.stop();
    this.loader.destroy();
    this.loader = null;
  }

  public void resize(int paramInt1, int paramInt2) {
    this.backBuffer = new BufferedImage(paramInt1, paramInt2, 1);
    this.image = new BufferedImage(paramInt1, paramInt2, 1);

    this.loader.setSize(paramInt1, paramInt2);

    this.loader.update(this.backBuffer.getGraphics());
    this.loader.paint(this.backBuffer.getGraphics());
  }

  public boolean setAccount(String paramString) {
    int i = 0;
    for (String str : AccountManager.getAccountNames()) {
      if (str.toLowerCase().equals(paramString.toLowerCase())) {
        i = 1;
      }
    }
    if (i != 0) {
      this.account = paramString;
      return true;
    }
    this.account = null;
    return false;
  }

  public void setPanel(Component paramComponent) {
    this.panel = paramComponent;
  }

  public void addListener(Class<?> paramClass) {
    EventListener localEventListener = instantiateListener(paramClass);
    this.listeners.put(paramClass.getName(), localEventListener);
    this.eventManager.addListener(localEventListener);
  }

  public void removeListener(Class<?> paramClass) {
    EventListener localEventListener = (EventListener)this.listeners.get(paramClass.getName());
    this.listeners.remove(paramClass.getName());
    this.eventManager.removeListener(localEventListener);
  }

  public boolean hasListener(Class<?> paramClass) {
    return (paramClass != null) && (this.listeners.get(paramClass.getName()) != null);
  }

  public String getAccountName() {
    return this.account;
  }

  public Client getClient() {
    return this.client;
  }

  public Canvas getCanvas() {
    if (this.client == null) {
      return null;
    }
    return (Canvas)this.client.getCanvas();
  }

  public Graphics getBufferGraphics() {
    Graphics localGraphics = this.backBuffer.getGraphics();
    this.paintEvent.graphics = localGraphics;
    this.textPaintEvent.graphics = localGraphics;
    this.textPaintEvent.idx = 0;
    this.eventManager.processEvent(this.paintEvent);
    this.eventManager.processEvent(this.textPaintEvent);
    localGraphics.dispose();
    this.image.getGraphics().drawImage(this.backBuffer, 0, 0, null);
    if (this.panel != null) {
      this.panel.repaint();
    }
    return this.backBuffer.getGraphics();
  }

  public BufferedImage getImage() {
    return this.image;
  }

  public BotStub getBotStub() {
    return this.botStub;
  }

  public RSLoader getLoader() {
    return this.loader;
  }

  public MethodContext getMethodContext() {
    return this.methods;
  }

  public EventManager getEventManager() {
    return this.eventManager;
  }

  public InputManager getInputManager() {
    return this.im;
  }

  public BreakHandler getBreakHandler() {
    return this.bh;
  }

  public ScriptHandler getScriptHandler() {
    return this.sh;
  }

  private void setClient(Client paramClient) {
    this.client = paramClient;
    this.client.setCallback(new CallbackImpl(this));
    this.methods = new MethodContext(this);
    this.sh.init();
  }
  private EventListener instantiateListener(Class<?> paramClass) {
    try {
      EventListener localEventListener;
      try {
        Constructor localConstructor = paramClass.getConstructor(new Class[] { Bot.class });
        localEventListener = (EventListener)localConstructor.newInstance(new Object[] { this });
      } catch (Exception localException2) {
        localEventListener = (EventListener)paramClass.asSubclass(EventListener.class).newInstance();
      }
      return localEventListener;
    } catch (Exception localException1) {
    }
    return null;
  }
}